package com.potevio.simulator.strategy;



import com.potevio.simulator.util.ClassResourcePatternResolver;
import com.potevio.simulator.util.DynamicEnumUtil;

import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

/**
 * @ClassName CommandEnum
 * @Description: 字段类型和接收报文Map<String, String>枚举类
 * @Date 2021/6/1  16:00
 * @Author zyd
 */
public enum CommandEnum {;

    private String command;
    private String clazz;
    public static Map<String, String> map = new HashMap<>();

    /**
     * 静态代码块，只需要在启动时候扫描一次stategy/impl下的所有实现类，放到map中即可，
     * 避免每次调用context.getInstance()时调用此方法扫描包下所有类名
     */
    static {
        System.out.println("加载静态代码块..........");
        Set<String> set = null;
        try {
            set = ClassResourcePatternResolver.findAllClassPathResources();
            Iterator<String> it = set.iterator();
            while (it.hasNext()){
                String fullName = it.next();
                String command = fullName.substring(fullName.lastIndexOf(".")+1).replace("Command","").toLowerCase().trim();
                DynamicEnumUtil.addEnum(CommandEnum.class,command,new Class<?>[] {String.class,String.class},new Object[] {command,fullName});
                for (CommandEnum value: CommandEnum.values()){
                    String cmd = value.getCommand();
                    String claz = value.getClazz();
                    map.put(command,claz);
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public String getCommand() {
        return command;
    }

    CommandEnum(String command, String clazz) {
        this.command = command;
        this.clazz = clazz;
    }

    public void setCommand(String command) {
        this.command = command;
    }

    public String getClazz() {
        return clazz;
    }

    public void setClazz(String clazz) {
        this.clazz = clazz;
    }

}
